﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.controls {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.css.*;
    import reprise.events.*;
    import flash.display.*;
    import reprise.utils.*;
    import flash.net.*;
    import flash.text.*;

    public class Label extends AbstractInput {

        protected var m_containsImages:Boolean;
        protected var m_labelXML:XML;
        protected var m_textSetExternally:Boolean;
        protected var m_cacheInvalid:Boolean;
        protected var m_overflowIsInvalid:Boolean;
        protected var m_labelDisplay:TextField;
        protected var m_bitmapCache:Bitmap;
        protected var m_lastHoverIndex:int;
        protected var m_textLinkHrefs:Array;
        protected var m_internalStyleIndex:int;
        protected var m_textAlignment:String;
        protected var m_usedLabelXML:XML;
        protected var m_nodesMap:Array;

        protected static var AS_LINK_PREFIX:String = "event:";

        protected function scrollbar_change(_arg1:Event):void{
            draw();
        }
        protected function cleanNode(_arg1:XML, _arg2:String, _arg3:CSS, _arg4:NodeCleanupConfig):void{
            var _local6:CSSDeclaration;
            var _local10:XML;
            var _local11:Boolean;
            var _local12:String;
            var _local13:int;
            var _local14:CSSProperty;
            var _local15:String;
            var _local16:String;
            var _local17:CSSProperty;
            var _local18:String;
            var _local19:String;
            var _local20:String;
            var _local21:Number;
            var _local22:CSSDeclaration;
            var _local23:String;
            var _local5:int = _arg4.index;
            if (_arg1.nodeKind() == "text"){
                _local11 = false;
                _local12 = _arg1.toString();
                if (_arg4.transform){
                    _local12 = transformText(_local12, _arg4.transform);
                    _local11 = true;
                };
                _local13 = 0;
                while (_local13 < (_local12.length - 1)) {
                    if (((!((" \n\r\t".indexOf(_local12.charAt(_local13)) == -1))) && (!((" \n\r\t".indexOf(_local12.charAt((_local13 + 1))) == -1))))){
                        _local11 = true;
                        _local12 = (_local12.substr(0, (_local13 + 1)) + _local12.substr((_local13 + 2)));
                        _local13--;
                    };
                    _local13++;
                };
                if (((_arg4.removeFirstWhitespace) && (!((" \n\r\t".indexOf(_local12.charAt(0)) == -1))))){
                    _local12 = _local12.substr(1);
                };
                if (_local12.length){
                    _arg4.removeFirstWhitespace = !((" \n\r\t".indexOf(_local12.charAt((_local12.length - 1))) == -1));
                };
                _arg4.index = (_arg4.index + _local12.length);
                if (_local11){
                    _arg1.parent().children()[_arg1.childIndex()] = _local12;
                };
                return;
            };
            if (_arg1.localName().toLowerCase() == "br"){
                _arg4.removeFirstWhitespace = true;
                _arg4.index++;
                return;
            };
            if (_arg1.hasOwnProperty("ignore")){
                return;
            };
            if (!_arg1.parent()){
                _local6 = m_complexStyles.clone();
                _local14 = _local6.getStyle("textTransform");
                ((_local14) && ((_arg4.transform = String(_local14.valueOf()))));
            } else {
                _local15 = _arg1.@["class"].toString();
                if (_local15.length){
                    _local15 = (("@." + _local15.split(" ").join("@.")) + "@");
                };
                _local16 = _arg1.@id.toString();
                if (_local16.length){
                    _local16 = (("@#" + _local16) + "@");
                    delete _arg1.@id;
                };
                _arg2 = (_arg2 + ((((" @" + _arg1.localName().toLowerCase()) + "@") + _local15) + _local16));
                _local6 = _arg3.getStyleForEscapedSelectorPath(_arg2);
            };
            var _local7:String = _arg1.@style.toString();
            if (_local7.length){
                _local6.mergeCSSDeclaration(CSSParsingHelper.parseDeclarationString(_local7, null));
                delete _arg1.@style;
            };
            var _local8:String = _local6.textStyleName((m_internalStyleIndex++ == 0));
            _arg1.@["class"] = _local8;
            if (m_textAlignment != "mixed"){
                _local17 = _local6.getStyle("textAlign");
                if (_local17){
                    _local18 = (_local17.valueOf() as String);
                } else {
                    _local18 = "left";
                };
                if (!m_textAlignment){
                    m_textAlignment = _local18;
                } else {
                    if (m_textAlignment != _local18){
                        m_textAlignment = "mixed";
                    };
                };
            };
            var _local9:Boolean;
            switch (_arg1.localName().toLowerCase()){
                case "a":
                    _local19 = _arg1.@href.toString();
                    if (_local19.length){
                        _local20 = _arg1.@target.toString();
                        if (_local20.length){
                            _local19 = (_local19 + ("|" + _local20));
                            delete _arg1.@target;
                        };
                        _arg1.@href = (AS_LINK_PREFIX + m_textLinkHrefs.length);
                        m_textLinkHrefs.push(_local19);
                    };
                    break;
                case "p":
                    if (!_arg1.parent()){
                        break;
                    };
                    _local9 = true;
                    if (_local6.hasStyle("marginBottom")){
                        _local21 = ((_local6.getStyle("marginBottom").valueOf() as Number) - 2);
                        _local7 = (("leading: " + _local21) + "; font-size: 1;");
                        _local22 = CSSParsingHelper.parseDeclarationString(_local7, null);
                        _local8 = _local22.textStyleName((m_internalStyleIndex++ == 0));
                        _arg1.parent().insertChildAfter(_arg1, new XML((("<p ignore=\"1\" class=\"" + _local8) + "\">&nbsp;</p>")));
                    };
                    break;
                case "span":
                case "ul":
                case "li":
                    break;
                case "img":
                    m_containsImages = true;
                    _local23 = (_local6.getStyle("display")) ? _local6.getStyle("display").specifiedValue() : "block";
                    if (_local23 == "block"){
                        _local9 = true;
                    };
                    break;
                default:
                    _arg1.setLocalName("span");
            };
            for each (_local10 in _arg1.children()) {
                cleanNode(_local10, _arg2, _arg3, _arg4);
            };
            if (_local9){
                _arg4.removeFirstWhitespace = true;
                _arg4.index++;
            };
            m_nodesMap.push({start:_local5, end:_arg4.index, path:_arg2, styleName:_local8, node:_arg1});
        }
        override protected function validateAfterChildren():void{
            m_stylesInvalidated = ((m_stylesInvalidated) || (m_overflowIsInvalid));
            super.validateAfterChildren();
        }
        override protected function createScrollbar(_arg1:String, _arg2:Boolean=true):Scrollbar{
            var _local3:Scrollbar = super.createScrollbar(_arg1, true);
            _local3.setScrollTarget(m_labelDisplay, _arg1);
            _local3.addEventListener(Event.CHANGE, scrollbar_change);
            return (_local3);
        }
        override protected function applyOverflowProperty():void{
            var _local6:int;
            var _local7:int;
            if (!m_overflowIsInvalid){
                return;
            };
            m_overflowIsInvalid = false;
            var _local1:* = m_currentStyles.overflowX;
            var _local2:* = m_currentStyles.overflowY;
            if ((((((((m_currentStyles.overflowX == null)) || ((_local1 == "visible")))) || ((_local1 == "hidden")))) && ((((((m_currentStyles.overflowY == null)) || ((_local2 == "visible")))) || ((_local2 == "hidden")))))){
                super.applyOverflowProperty();
                return;
            };
            var _local3:Number = m_currentStyles.width;
            var _local4:Number = Math.max(calculateContentHeight(), m_currentStyles.height);
            var _local5:Number = ((m_currentStyles.scrollbarWidth) || (DEFAULT_SCROLLBAR_WIDTH));
            if (_local2 == "scroll"){
                if (!m_vScrollbar){
                    m_vScrollbar = createScrollbar(Scrollbar.ORIENTATION_VERTICAL);
                };
                _local3 = (_local3 - _local5);
                m_vScrollbar.setVisibility(true);
            };
            if (_local1 == "scroll"){
                if (!m_hScrollbar){
                    m_hScrollbar = createScrollbar(Scrollbar.ORIENTATION_HORIZONTAL);
                };
                _local4 = (_local4 - _local5);
                m_hScrollbar.setVisibility(true);
            };
            if (_local2 == 0){
                m_labelDisplay.height = (_local4 + 4);
                m_labelDisplay.width = (_local3 + 4);
                _local6 = m_labelDisplay.maxScrollH;
                _local7 = m_labelDisplay.maxScrollV;
                if (_local7 > 1){
                    if (!m_vScrollbar){
                        m_vScrollbar = createScrollbar(Scrollbar.ORIENTATION_VERTICAL);
                    };
                    _local3 = (_local3 - _local5);
                    m_vScrollbar.setVisibility(true);
                    m_labelDisplay.width = (_local3 + 3);
                } else {
                    if (m_vScrollbar){
                        m_vScrollbar.setVisibility(false);
                    };
                };
            };
            if (_local1 == 0){
                if (((!(m_labelDisplay.wordWrap)) && ((_local6 > 0)))){
                    if (!m_hScrollbar){
                        m_hScrollbar = createScrollbar(Scrollbar.ORIENTATION_HORIZONTAL);
                    };
                    _local4 = (_local4 - _local5);
                    m_hScrollbar.setVisibility(true);
                    if (((((!(m_vScrollbar)) || (!(m_vScrollbar.visibility())))) && ((m_labelDisplay.maxScrollV > 1)))){
                        if (!m_vScrollbar){
                            m_vScrollbar = createScrollbar(Scrollbar.ORIENTATION_VERTICAL);
                        };
                        _local3 = (_local3 - _local5);
                        m_vScrollbar.setVisibility(true);
                    };
                } else {
                    if (m_hScrollbar){
                        m_hScrollbar.setVisibility(false);
                    };
                };
            };
            if (((!(((m_vScrollbar) && (m_vScrollbar.visibility())))) && (!(((m_hScrollbar) && (m_hScrollbar.visibility())))))){
                return;
            };
            if (((m_hScrollbar) && ((_local1 == "hidden")))){
                m_hScrollbar.setVisibility(false);
            };
            if (((m_vScrollbar) && ((_local2 == "hidden")))){
                m_vScrollbar.setVisibility(false);
            };
            m_labelDisplay.width = (_local3 + 6);
            m_labelDisplay.height = (_local4 + 6);
            _local4 = (_local4 + (m_currentStyles.paddingTop + m_currentStyles.paddingBottom));
            _local3 = (_local3 + (m_currentStyles.paddingLeft + m_currentStyles.paddingRight));
            if (m_vScrollbar){
                m_vScrollbar.height = _local4;
                m_vScrollbar.top = m_currentStyles.borderTopWidth;
                m_vScrollbar.left = (_local3 + m_currentStyles.borderLeftWidth);
                m_vScrollbar.delayValidation();
            };
            if (m_hScrollbar){
                m_hScrollbar.height = _local3;
                m_hScrollbar.top = (_local4 + _local5);
                m_hScrollbar.left = m_currentStyles.borderLeftWidth;
                m_hScrollbar.delayValidation();
            };
        }
        protected function labelDisplay_link(_arg1:TextEvent):void{
            _arg1.preventDefault();
            _arg1.stopImmediatePropagation();
            _arg1.stopPropagation();
        }
        override protected function applyInFlowChildPositions():void{
        }
        override protected function initialize():void{
            super.initialize();
            m_labelXML = <p/>
            ;
            m_textLinkHrefs = [];
            m_nodesMap = [];
        }
        public function set enabled(_arg1:Boolean):void{
            m_instanceStyles.setStyle("selectable", (_arg1) ? "true" : "false");
            m_labelDisplay.selectable = enabled;
            m_labelDisplay.mouseEnabled = enabled;
        }
        public function handleTextClick(_arg1:MouseEvent):Boolean{
            return (labelDisplay_click(_arg1));
        }
        override public function set opacity(_arg1:Number):void{
            var _local2:Number = ((m_currentStyles.opacity) || (-1));
            if (_arg1 == _local2){
                return;
            };
            super.opacity = _arg1;
            var _local3:Number = ((_local2 == 0)) ? 0 : ((_local2 < 1)) ? 1 : 2;
            var _local4:Number = ((_arg1 == 0)) ? 0 : ((_arg1 < 1)) ? 1 : 2;
            if (_local3 != _local4){
                draw();
            };
        }
        public function get textWidth():Number{
            return (m_labelDisplay.textWidth);
        }
        override protected function createChildren():void{
            m_labelDisplay = new TextField();
            m_labelDisplay = TextField(m_contentDisplay.addChild(m_labelDisplay));
            m_labelDisplay.name = "labelDisplay";
            m_labelDisplay.styleSheet = CSSDeclaration.TEXT_STYLESHEET;
            m_contentDisplay.addEventListener(MouseEvent.CLICK, labelDisplay_click);
            m_contentDisplay.addEventListener(MouseEvent.MOUSE_MOVE, contentDisplay_mouseMove);
            m_contentDisplay.addEventListener(MouseEvent.MOUSE_OUT, contentDisplay_mouseOut);
            m_labelDisplay.addEventListener(TextEvent.LINK, labelDisplay_link);
        }
        protected function applyLabel(_arg1:XML):void{
            var _local4:Number;
            var _local5:Number;
            var _local6:Number;
            var _local7:Number;
            XML.prettyPrinting = false;
            var _local2:String = _arg1.toXMLString();
            _local2 = _local2.substr(0, (_local2.length - 3));
            var _local3 = !((_local2 == m_labelDisplay.htmlText));
            if (((((!(_local3)) && ((m_oldContentBoxWidth == m_contentBoxWidth)))) && ((m_oldContentBoxHeight == m_contentBoxHeight)))){
                return;
            };
            m_cacheInvalid = true;
            if (m_currentStyles.width){
                m_labelDisplay.width = (m_currentStyles.width + 6);
            } else {
                m_labelDisplay.autoSize = "left";
            };
            if (_local3){
                m_labelDisplay.htmlText = _local2;
                m_usedLabelXML = _arg1;
            };
            if (m_labelDisplay.wordWrap){
                m_labelDisplay.autoSize = "left";
                _local4 = m_labelDisplay.height;
            } else {
                m_labelDisplay.height = (m_labelDisplay.textHeight + 8);
            };
            m_labelDisplay.autoSize = "none";
            m_overflowIsInvalid = true;
            if (((((!((m_textAlignment == "mixed"))) && (!(m_containsImages)))) && ((m_labelDisplay.textWidth < (m_labelDisplay.width - 10))))){
                _local5 = m_labelDisplay.textHeight;
                _local6 = m_labelDisplay.textWidth;
                _local7 = m_labelDisplay.width;
                m_labelDisplay.width = (m_labelDisplay.textWidth + 10);
                if (((!((m_labelDisplay.textHeight == _local5))) || (!((m_labelDisplay.textWidth == _local6))))){
                    m_labelDisplay.width = _local7;
                };
            };
        }
        public function setLabel(_arg1:String):void{
            XML.ignoreWhitespace = false;
            m_labelXML = new XML((("<p>" + _arg1) + "</p>"));
            XML.ignoreWhitespace = true;
            m_textSetExternally = true;
            invalidate();
        }
        public function get textHeight():Number{
            return (m_labelDisplay.textHeight);
        }
        override protected function validateChildren():void{
            renderLabel();
        }
        override public function setValue(_arg1):void{
            label = _arg1.toString();
        }
        protected function labelDisplay_click(_arg1:MouseEvent):Boolean{
            var _local9:URLRequest;
            var _local2:int = m_labelDisplay.getCharIndexAtPoint(m_labelDisplay.mouseX, m_labelDisplay.mouseY);
            if (((!(m_labelDisplay.length)) || ((_local2 == -1)))){
                return (false);
            };
            var _local3:TextFormat = m_labelDisplay.getTextFormat(_local2, (_local2 + 1));
            if (!_local3.url){
                return (false);
            };
            var _local4:Number = parseInt(_local3.url.substring(AS_LINK_PREFIX.length));
            var _local5:Array = String(m_textLinkHrefs[_local4]).split("|");
            var _local6:String = _local5[0];
            var _local7:String = _local5[1];
            var _local8:LabelEvent = new LabelEvent(LabelEvent.LINK_CLICK, true);
            _local8.href = _local6;
            _local8.linkTarget = _local7;
            dispatchEvent(_local8);
            if (!_local8.isDefaultPrevented()){
                _local9 = new URLRequest(_local8.href);
                navigateToURL(_local9, ((_local8.linkTarget) || ("_self")));
            };
            _arg1.stopImmediatePropagation();
            return (true);
        }
        public function getLabel():String{
            XML.prettyPrinting = false;
            var _local1:String = m_labelXML.toXMLString();
            return (_local1);
        }
        override protected function draw():void{
            if (((!(m_cacheInvalid)) && (!(m_dimensionsChanged)))){
                return;
            };
            m_cacheInvalid = false;
            applyRasterize();
        }
        protected function transformText(_arg1:String, _arg2:String):String{
            switch (_arg2){
                case "uppercase":
                    if (_arg1.indexOf("ß") != -1){
                        _arg1 = _arg1.split("ß").join("SS");
                    };
                    _arg1 = _arg1.toUpperCase();
                    break;
                case "lowercase":
                    _arg1 = _arg1.toLowerCase();
                    break;
                case "capitalize":
                    _arg1 = StringUtil.toTitleCase(_arg1);
                    break;
            };
            return (_arg1);
        }
        override protected function initDefaultStyles():void{
            m_elementDefaultStyles.setStyle("selectable", "true");
            m_elementDefaultStyles.setStyle("display", "inline");
            m_elementDefaultStyles.setStyle("wordWrap", "wrap");
            m_elementDefaultStyles.setStyle("multiline", "true");
        }
        override protected function parseXMLDefinition(_arg1:XML, _arg2:String):void{
            super.parseXMLDefinition(_arg1, _arg2);
            if (_arg1.localName().toLowerCase() != "p"){
                XML.prettyPrinting = false;
                m_labelXML = <p/>
                ;
                m_labelXML.setChildren(_arg1);
            } else {
                m_labelXML = _arg1;
            };
        }
        public function get enabled():Boolean{
            return (m_currentStyles.selectable);
        }
        override protected function applyOutOfFlowChildPositions():void{
        }
        override protected function measure():void{
            if (m_labelDisplay.text == ""){
                m_intrinsicWidth = 0;
                m_intrinsicHeight = 0;
            } else {
                m_intrinsicWidth = Math.ceil(m_labelDisplay.textWidth);
                m_intrinsicHeight = Math.ceil((m_labelDisplay.height - 4));
            };
        }
        protected function contentDisplay_mouseOut(_arg1:MouseEvent):void{
            updateHover(true);
        }
        protected function contentDisplay_mouseMove(_arg1:MouseEvent):void{
            updateHover();
        }
        public function set label(_arg1:String):void{
            setLabel(_arg1);
        }
        protected function applyRasterize():void{
            var _local1:BitmapData;
            if (m_bitmapCache){
                m_contentDisplay.removeChild(m_bitmapCache);
                m_bitmapCache = null;
            };
            if (((!(m_currentStyles.embedFonts)) && ((((m_currentStyles.opacity < 1)) || (m_currentStyles.rasterizeDeviceFonts))))){
                m_labelDisplay.visible = false;
                if ((((((m_currentStyles.opacity == 0)) || (!(m_labelDisplay.width)))) || (!(m_labelDisplay.height)))){
                    m_cacheInvalid = true;
                    return;
                };
                _local1 = new BitmapData(m_labelDisplay.width, m_labelDisplay.height, true, 0);
                _local1.draw(m_labelDisplay, null, null, null, null, true);
                m_bitmapCache = new Bitmap(_local1, "auto", true);
                m_contentDisplay.addChild(m_bitmapCache);
                m_bitmapCache.x = m_labelDisplay.x;
                m_bitmapCache.y = m_labelDisplay.y;
            } else {
                m_labelDisplay.visible = true;
            };
        }
        override protected function applyStyles():void{
            super.applyStyles();
            m_labelDisplay.selectable = m_currentStyles.selectable;
            m_labelDisplay.mouseEnabled = m_currentStyles.selectable;
            if (m_currentStyles.color){
                m_labelDisplay.alpha = m_currentStyles.color.opacity();
            } else {
                m_labelDisplay.alpha = 1;
            };
            m_labelDisplay.embedFonts = m_currentStyles.embedFonts;
            m_labelDisplay.antiAliasType = ((m_currentStyles.antiAliasType) || (AntiAliasType.NORMAL));
            if (m_labelDisplay.antiAliasType == AntiAliasType.ADVANCED){
                m_labelDisplay.gridFitType = ((m_currentStyles.gridFitType) || (GridFitType.PIXEL));
                m_labelDisplay.sharpness = ((m_currentStyles.sharpness) || (0));
                m_labelDisplay.thickness = ((m_currentStyles.thickness) || (0));
            };
            m_labelDisplay.wordWrap = (m_currentStyles.wordWrap == "wrap");
            m_labelDisplay.multiline = m_currentStyles.multiline;
        }
        override public function value(){
            return (label);
        }
        protected function updateHover(_arg1:Boolean=false):void{
            var _local2:Boolean;
            var _local5:Object;
            var _local6:XML;
            var _local7:CSSDeclaration;
            var _local8:Number;
            var _local9:Number;
            var _local3 = -1;
            if (!_arg1){
                _local3 = m_labelDisplay.getCharIndexAtPoint(m_labelDisplay.mouseX, m_labelDisplay.mouseY);
            };
            if (_local3 == m_lastHoverIndex){
                return;
            };
            var _local4:int = m_nodesMap.length;
            while (_local4--) {
                _local5 = m_nodesMap[_local4];
                _local6 = _local5.node;
                if ((((int(m_nodesMap[_local4].start) <= _local3)) && ((int(m_nodesMap[_local4].end) > _local3)))){
                    if (!_local5.hover){
                        _local5.hover = true;
                        _local5.defaultClass = _local6.@["class"].toString();
                        _local7 = m_rootElement.styleSheet.getStyleForEscapedSelectorPath((_local5.path + "@:hover@"));
                        _local6.@["class"] = _local7.textStyleName((_local6.parent() == null));
                        _local2 = true;
                    };
                } else {
                    if (_local5.hover){
                        _local5.hover = false;
                        _local6.@["class"] = _local5.defaultClass;
                        _local2 = true;
                    };
                };
            };
            if (_local2){
                _local8 = m_labelDisplay.width;
                _local9 = m_labelDisplay.height;
                applyLabel(m_usedLabelXML);
                m_labelDisplay.width = _local8;
                m_labelDisplay.height = _local9;
                if (m_overflowIsInvalid){
                    invalidate();
                };
            };
            m_lastHoverIndex = _local3;
        }
        public function get label():String{
            return (getLabel());
        }
        override protected function parseXMLContent(_arg1:XMLList):void{
        }
        protected function renderLabel():void{
            var labelXML:XML;
            if (!((m_stylesInvalidated) || (m_textSetExternally))){
                return;
            };
            m_internalStyleIndex = 0;
            m_textAlignment = null;
            m_containsImages = false;
            XML.prettyPrinting = false;
            var labelString:String = m_labelXML.toXMLString();
            labelString = resolveBindings(labelString);
            XML.ignoreWhitespace = false;
            try {
                labelXML = new XML(labelString);
            } catch(error:Error) {
                labelXML = new XML("<p style=\"color: red;\">malformed content</p>");
            };
            XML.ignoreWhitespace = true;
            m_nodesMap.length = 0;
            cleanNode(labelXML, m_selectorPath, m_rootElement.styleSheet, new NodeCleanupConfig());
            m_textSetExternally = false;
            applyLabel(labelXML);
            m_labelDisplay.y = (m_currentStyles.paddingTop - 2);
            if (m_textAlignment == "left"){
                m_labelDisplay.x = (m_currentStyles.paddingLeft - 2);
            } else {
                if (m_textAlignment == "right"){
                    m_labelDisplay.x = (((m_currentStyles.paddingLeft + m_currentStyles.width) - m_labelDisplay.width) + 2);
                } else {
                    if (m_textAlignment == "center"){
                        m_labelDisplay.x = (m_currentStyles.paddingLeft + Math.round(((m_currentStyles.width / 2) - (m_labelDisplay.width / 2))));
                    };
                };
            };
        }

    }
}//package reprise.controls 

final class NodeCleanupConfig {

    public var index:int;// = 0
    public var removeFirstWhitespace:Boolean = true
    public var transform:String;

    public function NodeCleanupConfig(){
    }
}
